package com.hq.ims.data.utils;

import com.hq.utils.exception.CommonException;
import com.hq.utils.util.FileUtils;
import com.hq.utils.util.JsonUtils;
import com.hq.utils.util.StringUtils;

import java.util.HashMap;

/**
 * lichaojie
 *
 * @ClassName AddressUtil
 **/
public class AddressUtil {

    public static String getCompleteAddress(String provinceCode, String cityCode, String districtCode, String street) {
        String content = FileUtils.readFileContent(Thread.currentThread().getContextClassLoader().getResourceAsStream(LocalConstant.CONFIG_DICT_ADDRESS_JSON_FILE_NAME));

        HashMap hashMap = JsonUtils.stringToObject(content, HashMap.class);
        if (hashMap == null || hashMap.size() < 0) {
            throw CommonException.build("省市区数据不存在！");
        }
        Object provinceObj = hashMap.get(provinceCode);
        String province = provinceObj == null ? StringUtils.EMPTY : provinceObj.toString();

        Object cityObj = hashMap.get(cityCode);
        String city = cityObj == null ? StringUtils.EMPTY : cityObj.toString();

        Object districtObj = hashMap.get(districtCode);
        String district = districtObj == null ? StringUtils.EMPTY : districtObj.toString();
        street = street == null ? StringUtils.EMPTY : street;
        //拼接完整地址
        String addressFullName = new StringBuffer().append(province).append(city).append(district).append(street).toString();

        return addressFullName;
    }

    /**
     * @param redisKey redisKey
     * @param cityCode 市
     * @return : java.lang.String
     * <p>
     * <p>
     * 拼接redisKey
     */
    public static String splicingRedisKey(String redisKey, String cityCode) {
        if (StringUtils.isEmpty(cityCode)) {
            return redisKey;
        }
        return redisKey + "_" + cityCode;
    }
}
